package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.common.uitl.JsonUtils;
import com.jrx.anytxn.customer.bean.CustomerInfoBean;
import com.jrx.anytxn.customer.constant.CustomerConstant;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.ribbon.config.GrayVersionConfig;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liuyu
 * @Description
 */
@Order(200)
@Service
public class IdInvalidHandle extends BaseBatchAccoutHandle<CustomerInfoBean> {

	private static final Logger logger = LoggerFactory.getLogger(IdInvalidHandle.class);

	@Autowired
	private RocketMQTemplate rocketMQTemplate;
	@Autowired
	private GrayVersionConfig grayVersionConfig;

	@Override
	public List<CustomerInfoBean> handBatchAccout() throws Exception {
		try {
			CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
			CmCustomerFirstLevel firstLevel = custBatchSaveBean.getDealCust();
			String idExpireDate = firstLevel.getIdExpireDate();
			Date nextProcessingDate = firstLevel.getNextProcessingDate();
			Date date = DateUtils.parseDate(idExpireDate);
			if (null == date) {
				logger.info("客户id：{} ==> 身份证过期时间：{}", firstLevel.getCustomerId(), idExpireDate);
				return null;
			}
			int days = DateUtils.getDiffDays(nextProcessingDate, date);
			// 大于当前日期并且两个日期差值=10天或0天，发送Mq消息
			if (days == 10 || days == 0) {
				CustomerInfoBean bean = new CustomerInfoBean();
				bean.setModifyCmCustomerFirstLevel(firstLevel);
				String jsonMessage = JsonUtils.toJSon(bean);
				rocketMQTemplate.syncSend(CustomerConstant.MQ_ID_INVALID_TOPIC + grayVersionConfig.getCurrentVersion(),
						MessageBuilder.withPayload(jsonMessage).build(), 5000, 3);
			}
		}catch (Exception e) {
			logger.error("身份证有效期处理异常：", e);
		}

		return null;
	}

}